<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改角色</title>
<link href="${ctx }/css/ztreeStyle.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="${ctx }/js/niceValidator/jquery.validator.css" />
<link href="${ctx }/css/ztree/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
<link href="${ctx }/js/layui/css/layui.css" rel="stylesheet" type="text/css" />
<link href="${ctx }/css/ztree/demo.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx }/js/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="${ctx }/js/layer/layer.js"></script>
<script type="text/javascript" src="${ctx }/js/niceValidator/jquery.validator.js"></script>
<script type="text/javascript" src="${ctx }/js/niceValidator/local/zh_CN.js"></script>
<script type="text/javascript" src="${ctx }/js/common.js"></script>
<script type="text/javascript" src="${ctx }/js/ztree/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="${ctx }/js/ztree/jquery.ztree.excheck-3.5.js"></script>
</head>
<body style="min-width:900px;">
<div class="listNavTitle">
    <p>
        <label>系统管理 ></label>
        <label>角色管理</label>
    </p>
</div>
<div class="formbody">
  <div class="formtitle"><span>角色修改</span></div>	
 <form id="submitForm">
 <input type="hidden" name="roleCode" value="${item.roleCode}"/>
  <ul class="forminfo">
   <li>
      <label>角色名称</label>
      <input name="roleName" value="${item.roleName}" type="text" class="dfinput"  data-rule="角色名称:required;"/>
    </li>
      <li>
          <label>角色模块</label>
          <select name="roleModuleType" onchange="roleTypeChange();" id="roleModuleType">
              <option value="sysman" <c:if test="${item.roleModuleType == 'sysman'}">selected="selected"</c:if>  >系统管理</option>
              <option value="enterprise" <c:if test="${item.roleModuleType == 'enterprise'}">selected="enterprise"</c:if>>企业角色</option>
              <option value="security" <c:if test="${item.roleModuleType == 'security'}">selected="security"</c:if>>公安角色</option>
          </select>
      </li>
    <li>
      <label>角色授权</label>
      <div>
      	<input type="hidden" name="authResourceStr" id="authResourceStr" />
      	<ul id="dleft_tab1" class="ztree" style="width:335px;height:200px;"></ul>
      </div>
    </li>
	<li>
      <label>标识CODE</label>
      <input name="roleShrioCode" value="${item.roleShrioCode}" type="text" class="dfinput"  data-rule="标识CODE:required;"/>
    </li>
    <li>
      <label>角色描述</label>
      <textarea name="roleDes" style="width:500px;height:200px;border:1px solid black">${item.roleDes}</textarea>
    </li>
    <li>
      <label>&nbsp;</label>
        <button class="layui-btn layui-btn-normal" type="button" class="btn" id="submitBtn">确认保存</button>
    </li>
  </ul>
</form>
</div>
</body>
</html>
<script>
	$(document).ready(function(){
		$("#submitBtn").click(function(){
			submitForms();
		});
		roleAuthData();
		onloadMenu();
	});
    function roleTypeChange(){
        onloadMenu();
    }
	var index = parent.layer.getFrameIndex(window.name);
	//修改提交
	function submitForms(){
		$("#submitForm").trigger("validate");
		instance = $("#submitForm").data("validator");
		if(instance.isFormValid()){
			selectQuan();
			if($("#authResourceStr").val().trim()==''){
				layer.alert('请选择权限', {icon: 2});
				return;
			}
			var loadi = layer.load();
			var datajson=$("#submitForm").serialize();
			$.ajax({
				type:"POST",
				url:"${ctx}/sysman/systemRole/upChange.do",
				data:datajson,
				dataType:"json",
				success:function(data){
					layer.close(loadi);
					if(data.status=='SUCCESS'){
						window.parent.baseSearchQuery('Refresh');
						window.parent.baseBottomMsgOpen('修改成功');
						window.parent.layer.close(index);
					}else{
						layer.alert(data.msg);
					}
				}
			});
		}
	}
	var roleAuths = null;
	function onloadMenu(){
        var roleModuleType = $("#roleModuleType option:selected").val();
	    var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};
	var jsondata="[";
	$.ajax({
			type:"POST",
			url:"${ctx }/sysman/authResource/authResourceAllList.do?moduleType="+roleModuleType,
			dataType:"json",
			async:false,
			success:function(data){
				console.log(data);
				for(var i=0;i<data.length;i++){
					var auth = data[i];
					var pass = false;
					for(var j = 0;j<roleAuths.length;j++){
						var roleAuth = roleAuths[j];
						if(auth.authCode == roleAuth.authCode){
							pass  = true;
							break;
						}
					}
					if(pass){
						jsondata+="{'pId':'"+auth.authparentCode+"','id':'"+auth.authCode+"','name':'"+auth.authName+"','checked':true},";
					}else{
						jsondata+="{'pId':'"+auth.authparentCode+"','id':'"+auth.authCode+"','name':'"+auth.authName+"'},";
					}
				}
			}
		});
		jsondata = jsondata.substring(0,jsondata.length-1);
		jsondata+="]";
		jsondata=eval(jsondata);
		$.fn.zTree.init($("#dleft_tab1"), setting, jsondata);
		var zTree = $.fn.zTree.getZTreeObj("dleft_tab1");
		zTree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
	}
	
	function roleAuthData(){
		$.ajax({
			type:"POST",
			url:"${ctx}/sysman/systemRoleAuth/systemRoleAuthAjaxList.do?roleCode=${item.roleCode}",
			dataType:"json",
			async:false,
			success:function(data){
				roleAuths = data;
			}
		});
	}
	
	function selectQuan(){
		var treeObj = $.fn.zTree.getZTreeObj("dleft_tab1");
	    var nodes = treeObj.getCheckedNodes(true);
		var nodeStr="";
		for(var i=0;i<nodes.length;i++){
			nodeStr+=nodes[i].id+",";
		}
		nodeStr = nodeStr.substring(0,nodeStr.length-1);
		$("#authResourceStr").val(nodeStr);
	}
</script>
